Completed
Push — master ( 40c2b4...09bd06 )
by Justin
01:29
created

Person.addFact   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
var Subject = require('./Subject'),
2
    Gender = require('./Gender'),
3
    Name = require('./Name'),
4
    Fact = require('./Fact'),
5
    utils = require('./utils');
6
    
7
/**
8
 * A person.
9
 * 
10
 * @constructor
11
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
12
 */
13
var Person = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof Person)){
17
    return new Person(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(Person.isInstance(json)){
22
    return json;
23
  }
24
  
25
  Subject.call(this, json);
26
  
27
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
28
    this.setPrivate(json.private);
29
    this.setGender(json.gender);
30
    this.setNames(json.names);
31
    this.setFacts(json.facts);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
32
  }
33
};
34
35
Person.prototype = Object.create(Subject.prototype);
36
37
Person._gedxClass = Person.prototype._gedxClass = 'GedcomX.Person';
38
39
/**
40
 * Check whether the given object is an instance of this class.
41
 * 
42
 * @param {Object} obj
43
 * @returns {Boolean}
44
 */
45
Person.isInstance = function(obj){
46
  return utils.isInstance(obj, this._gedxClass);
47
};
48
49
/**
50
 * Check whether the person is marked as private
51
 * 
52
 * @returns {Boolean} private
53
 */
54
Person.prototype.isPrivate = function(){
55
  return !!this.private;
56
};
57
58
/**
59
 * Set the private flag
60
 * 
61
 * @param {Boolean} isPrivate
62
 * @returns {Person} This instance
63
 */
64
Person.prototype.setPrivate = function(isPrivate){
65
  this.private = isPrivate;
66
  return this;
67
};
68
69
/**
70
 * Get the person's gender
71
 * 
72
 * @returns {Gender} gender
73
 */
74
Person.prototype.getGender = function(){
75
  return this.gender;
76
};
77
78
/**
79
 * Set the person's gender
80
 * 
81
 * @param {Gender} gender
82
 * @returns {Person} This instance
83
 */
84
Person.prototype.setGender = function(gender){
85
  if(gender){
86
    this.gender = Gender(gender);
87
  }
88
  return this;
89
};
90
91
/**
92
 * Get the names
93
 * 
94
 * @return {Name[]}
95
 */
96
Person.prototype.getNames = function(){
97
  return this.names || [];
98
};
99
100
/**
101
 * Set the names
102
 * 
103
 * @param {Name[]|Object[]} names
104
 * @returns {Person} This instance
105
 */
106
Person.prototype.setNames = function(names){
107
  return this._setArray(names, 'names', 'addName');
108
};
109
110
/**
111
 * Add a name
112
 * 
113
 * @param {NameForm|Object} name
114
 * @returns {Person} This instance
115
 */
116
Person.prototype.addName = function(name){
117
  return this._arrayPush(name, 'names', Name);
118
};
119
120
/**
121
 * Get the facts
122
 * 
123
 * @return {Fact[]}
124
 */
125
Person.prototype.getFacts = function(){
126
  return this.facts || [];
127
};
128
129
/**
130
 * Set the facts
131
 * 
132
 * @param {Fact[]|Object[]} facts
133
 * @returns {Person} This instance
134
 */
135
Person.prototype.setFacts = function(facts){
136
  return this._setArray(facts, 'facts', 'addFact');
137
};
138
139
/**
140
 * Add a fact
141
 * 
142
 * @param {Fact|Object} fact
143
 * @returns {Person} This instance
144
 */
145
Person.prototype.addFact = function(fact){
146
  return this._arrayPush(fact, 'facts', Fact);
147
};
148
149
/**
150
 * Export the object as JSON
151
 * 
152
 * @return {Object} JSON object
153
 */
154
Person.prototype.toJSON = function(){
155
  return this._toJSON(Subject, [
156
    'private',
157
    'gender',
158
    'names',
159
    'facts'
160
  ]);
161
};
162
163
module.exports = Person;